class PeopleController < ApplicationController

  def show
    @person = Person.find(params[:id])
    @transaction  = @person.transactions.build
    @transactions  = @person.transactions
  end

  $person_groups.each_with_index do |meth, index|
    define_method("#{meth}s") { 
      @people = Person.find_all_by_group(index)
      @new_person = Person.new(:group=> index)
      render "index"
    }
  end

  def edit
    @person = Person.find(params[:id])
  end

  def create
    @person = Person.new(params[:person])
    respond_to do |format|
      if @person.save
        @transaction  = @person.transactions.build
        @transactions  = @person.transactions
        @people = Person.find_all_by_group(@person.group)
        @new_person = Person.new(:group=>@person.group)
        format.html { render "index", :formats => [:html]}
      else
        format.js { render  "create_fail" }
      end
    end
  end

  def update
    @person = Person.find(params[:id])

    respond_to do |format|
      if @person.update_attributes(params[:person])
        format.html { redirect_to @person, notice: 'Person was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @person = Person.find(params[:id])
    @person.destroy

    respond_to do |format|
      format.html { redirect_to people_url }
      format.json { head :no_content }
    end
  end
end
